package com.qf.microuser.point.controller;

import com.qf.common.core.common.R;
import com.qf.common.xxljob.jobInfo.XxlJobInfo;
import com.qf.common.xxljob.utils.CronUtils;
import com.qf.common.xxljob.utils.XxlJobUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * --- 缘分让我们相遇乱世以外 ---
 *
 * @author SJY
 * 2025/1/3
 * 模拟,不建议这么写,不能在controller层调用定时任务,建议在service层调用
 */
@RestController
public class TestController {
    @Resource
    private XxlJobUtil xxlJobUtil;

    /**
     * 模拟实现定时发布公告
     */
    @GetMapping("/test")
    public R publicNotice(String title, String content, String time) {
        // 将公告信息写入数据库,公告的状态是不可见的

        // 如果需要定时发布,根据时间动态调用xxljob的接口生成任务
        XxlJobInfo info = new XxlJobInfo();
        info.setExecutorBlockStrategy("SERIAL_EXECUTION");
        info.setAuthor("sjy");
        info.setScheduleConf(CronUtils.onlyOnce(time));
        info.setExecutorRouteStrategy("FIRST");
        info.setTriggerStatus(0);
        info.setJobDesc("test动态生成任务");
        info.setMisfireStrategy("DO_NOTHING");
        info.setScheduleType("CRON");
        info.setGlueType("BEAN");
        info.setExecutorHandler("testMyJob");// 执行修改公告状态的任务
        info.setExecutorFailRetryCount(0);
        info.setExecutorTimeout(0);
        xxlJobUtil.addAndStart(info);
        return R.ok();
    }
}
